在大家都依照之前的要求建好需要的 ViewController 和 tableViewCell 後
我相信大家應該沒這麼快忘記設定 Realm 資料庫吧!
今天帶大家來實作鬧鐘要用的 model 吧!( 昨天說的 MVC 架構 )
同樣的我們要先設好 import 以及資料庫的 class
import Foundation
import RealmSwift
class AlarmData: Object {
    ...
}
依照昨天我們專案的需求我們需要設定的有
這些參數,所以我們一一填入
    //鬧鐘要響的時間
    @Persisted var alarmTime: String = "" 
    //創建鬧鐘的時間
    @Persisted var creatTime: String = "" 
    //設定的鬧鐘名稱
    @Persisted var name: String = "" 
    //設定重複天數的 bool
    @Persisted var repeatDays: List<Bool> = List<Bool>() 
    //選擇鬧鐘響鈴聲的
    @Persisted var sound: String = "" 
    //控制鬧鐘的開關
    @Persisted var isEnabled: Bool = true 
    //控制鬧鐘的稍後提醒
    @Persisted var isSnoozeOn: Bool = true 
在設定好後我們做一個簡易初始化器讓其他頁面也可以使用這些參數
    convenience init(alarmTime: String, 
                     creatTime: String, 
                     name: String, 
                     repeatDays: [Bool] = Array(repeating: false, 
                                                count: 7), 
                     sound: String, 
                     isSnoozeOn: Bool = true) {
        
            self.init()
            self.alarmTime = alarmTime
            self.creatTime = creatTime
            self.name = name
            self.sound = sound
            self.repeatDays.append(objectsIn: repeatDays)
            self.isSnoozeOn = isSnoozeOn
        
        }
接下來我們來設定要讓使用者可以跳通知選項
要先去 AppDelegate 請求權限
先 import UserNotifications 的套件
import UserNotifications
接著要在 class 也設定 UNUserNotificationCenterDelegate
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate{
    ...
}
最後在 class 裡新增一個 @objc
    @objc func userNotificationCenter(_ center: UNUserNotificationCenter, 
    willPresent notification: UNNotification, 
    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        NotificationCenter.default.post(name: NSNotification.Name("AlarmDidFire"), 
                                        object: notification)
        completionHandler([.alert, 
                           .sound, 
                           .badge])
    }
接著來到 MainView 裡,我們要來設定一個 func 來做請求通知權限
一樣先來 import UserNotifications
import UserNotifications
接著設定 func 請求權限
    func requestNotificationPermission() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
            if granted {
                print("Notification permission granted")
            } else {
                print("Notification permission denied")
            }
        }
    }
最後在包進 viewDidLoad 中就完成啦!